export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        try {
            const { sheet, row: addRow, rowCount: addRowCount = 1, table } = options;
            sheet.suspendPaint();
            const { row: dataRow } = table.dataRange();
            if (isUndo) {
                const { name, row, oldCol, rowCount, colCount, columns, cellTypes, bindingPath, showHeader,addRow, rowHeight } = options.old;
                sheet.addRows(addRow,1);
                sheet.setRowHeight(addRow,rowHeight);
                sheet.tables.resize(table,rowCount, colCount);
                console.log(options._oldValue, "options._oldValue");
                for (let col = oldCol;  col < colCount; col++) {
                    let cell = sheet.getCell(addRow, col);
                    console.log(options._oldCellTypes[addRow][col])
                    cell.cellType(options._oldCellTypes[addRow][col])
                        .backColor(options._oldBackColor[addRow][col])
                        .textIndent(options._oldTextIndent[addRow][col])
                        .backgroundImageLayout(options._oldBackgroundImageLayout[addRow][col])
                        .backgroundImage(options._oldBackGroundImage[addRow][col])
                        .font(options._oldFont[addRow][col])
                        .value(options._oldValue[addRow][col]);
                
            }
            if(options._oldSpans) {
                options._oldSpans.forEach(range => {
                    sheet.addSpan(range.row, range.col, range.rowCount, range.colCount);
                });
            }
            } else {
                const { row, col:oldCol, name, rowCount, colCount, columns, bindingPath,showHeader } = table.toJSON();
                options.old = {
                    name, row, oldCol, rowCount, colCount, columns, bindingPath, showHeader,addRow, rowHeight : sheet.getRowHeight(addRow)
                };
                options._oldBackColor = {};
                options._oldCellTypes = {};
                options._oldBackGroundImage = {};
                options._oldBackgroundImageLayout = {};
                options._oldTextIndent = {};
                options._oldValue = {};
                options._oldFont = {};
                    for (let col = oldCol; col < colCount; col++) {
                        if (!options._oldBackColor[addRow]) {
                            options._oldBackColor[addRow] = {}
                        }
                        if (!options._oldCellTypes[addRow]) {
                            options._oldCellTypes[addRow] = {};
                        }
                        if (!options._oldTextIndent[addRow]) {
                            options._oldTextIndent[addRow] = {};
                        }
                        if (!options._oldBackGroundImage[addRow]) {
                            options._oldBackGroundImage[addRow] = {};
                        }
                        if (!options._oldBackgroundImageLayout[addRow]) {
                            options._oldBackgroundImageLayout[addRow] = {};
                        }
                        if (!options._oldFont[addRow]) {
                            options._oldFont[addRow] = {};
                        }
                        if (!options._oldValue[addRow]) {
                            options._oldValue[addRow] = {};
                        }
                            let cell = sheet.getCell(addRow, col);
                            
                            options._oldBackColor[addRow][col] = cell.backColor();
                            options._oldCellTypes[addRow][col] = cell.cellType();
                            options._oldTextIndent[addRow][col] = cell.textIndent();
                            options._oldBackGroundImage[addRow][col] = cell.backgroundImage();
                            options._oldBackgroundImageLayout[addRow][col] = cell.backgroundImageLayout();
                            options._oldFont[addRow][col] = cell.font();
                            options._oldValue[addRow][col] = cell.value();
                            const span = sheet.getSpan(addRow, col);
                            if (span && span.row === addRow && span.col === col) {
                                if (!options._oldSpans) {
                                    options._oldSpans = [];
                                }
                                options._oldSpans.push(span);
                            }
                        
                    }
                
                sheet.deleteRows(addRow,addRowCount);
                sheet.tables.resize(table,rowCount-addRowCount, colCount);
            }
            sheet.resumePaint();
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
    }
};